//
//  HYLPageScroll.m
//  UIScrollView测试
//
//  Created by hyl on 16/5/2.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "HYLPageScroll.h"

@interface HYLPageScroll ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UIScrollView *pageScroll;
@property (strong,nonatomic) NSTimer *timer;
@end

@implementation HYLPageScroll
#pragma mark - 程序初使化
+(instancetype)pageScroll{
    NSLog(@"(instancetype)pageScroll");
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil]firstObject];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self=[super initWithCoder:aDecoder]) {
        NSLog(@"initWithCoder");
    }
    return self;
}
/**
 *
 *alloc --> init 会调用
 *当使用alloc-->init 初使化nib是不可以的，那么可以通过这个地方手写代码创建
 */
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        NSLog(@"initWithFrame");
    }
    return self;
}

#pragma mark - setter
-(void)setPictures:(NSArray *)pictures{
    _pictures=pictures;
    [self.pageScroll.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (int i=0; i<pictures.count; i++) {
        UIImageView *imageView=[[UIImageView alloc]init];
        imageView.image=[UIImage imageNamed:pictures[i]];
        [self.pageScroll addSubview:imageView];
    }
    self.pageScroll.pagingEnabled=YES;
    self.pageScroll.showsHorizontalScrollIndicator=NO;
    self.pageControl.numberOfPages=pictures.count;
    self.pageScroll.delegate=self;//设置代理
}
-(void)setOtherClor:(UIColor *)otherClor{
    _otherClor=otherClor;
    self.pageControl.pageIndicatorTintColor=otherClor;
}
-(void)setCurrentClor:(UIColor *)currentClor{
    _currentClor=currentClor;
    self.pageControl.currentPageIndicatorTintColor=currentClor;
}
#pragma mark - layoutSubviews
-(void)layoutSubviews{
    [super layoutSubviews];
    self.pageScroll.frame=self.bounds;
    CGFloat scrollWidth=self.pageScroll.frame.size.width;
    CGFloat scrollHeight=self.pageScroll.frame.size.height;
    CGFloat controlWidth=100;
    CGFloat controlHeight=20;
    for (int i=0; i<self.pageScroll.subviews.count; i++) {
        self.pageScroll.subviews[i].frame=CGRectMake(i*scrollWidth, 0, scrollWidth, scrollHeight);
    }
    self.pageScroll.contentSize=CGSizeMake(self.pictures.count*scrollWidth, 0);
    self.pageControl.frame=CGRectMake(scrollWidth-controlWidth, scrollHeight-controlHeight, controlWidth,controlHeight);
    NSLog(@"layoutSubviews");
}
/** 监视 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.pageControl.currentPage=((scrollView.contentOffset.x/self.pageScroll.frame.size.width)+0.5);
}

/** 初使完后会执行这个方法，所以在这里可以做一些初使化 */
-(void)awakeFromNib{
    NSLog(@"awakeFromNib");
    [self startTimer];
}
#pragma mark - timer控制器
-(void) nextPage{
    NSInteger page=self.pageControl.currentPage+1;
    if (page==self.pageControl.numberOfPages) {
        page=0;
    }
    CGPoint offset=self.pageScroll.contentOffset;
    offset.x=page*self.pageScroll.frame.size.width;
    [self.pageScroll setContentOffset:offset animated:YES];
    NSLog(@"nextPage");
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self stopTimer];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self startTimer];
}

-(void) stopTimer{
    [self.timer invalidate];
    self.timer=nil;
}
-(void) startTimer{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}
@end

